﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

using Common;

namespace com.globalwave.a8ext
{
    public partial class RewardPage : Common.BasePage
    {
        public RewardPage()
            : base()
        {
            entityName = "Reward";
        }


        public void list()
        {

            NameValueCollection criterion = getCriterion();

            DataSet ds = RewardService.getInstance().query(criterion, pageIndex, pageSize, null);

            ds.ExtendedProperties.Add("pageIndex", this.pageIndex.ToString());
            XmlDocument doc = U.toListXML(ds);
            XmlNodeList dateNodeList = doc.SelectNodes("//eff_date");
            foreach (XmlNode dateNode in dateNodeList)
            {
                if (dateNode.InnerText != null && dateNode.InnerText.Length > 10)
                {
                    dateNode.InnerText = dateNode.InnerText.Substring(0, 10);
                }
            }
            dateNodeList = doc.SelectNodes("//exp_date");
            foreach (XmlNode dateNode in dateNodeList)
            {
                if (dateNode.InnerText != null && dateNode.InnerText.Length > 10)
                {
                    dateNode.InnerText = dateNode.InnerText.Substring(0, 10);
                }
            }
            renderXML(doc.OuterXml);
        }

        public void create()
        {
            NameValueCollection reward = getReward();

            int reward_id =
                RewardService.getInstance().create(reward);

            reward.Set("reward_id", reward_id.ToString());

            renderSuccessCreateMessage(reward);

        }
         

        public void delete()
        {
            string[] reward_ids = this.getReward_ids();

            if (reward_ids == null)
            {
                RewardService.getInstance().delete(getReward());
            }
            else
            {
                RewardService.getInstance().deleteAll(reward_ids);
            }

            renderSuccessDeleteMessage();

        }

        public void update()
        {

            NameValueCollection reward = getReward();

            RewardService.getInstance().update(reward);


            renderSuccessUpdateMessage(reward);

        }

        public void queryEmployeeReward() {
            IList list = 
            RewardService.getInstance().queryEmployeeReward(
                Request.Params["employee_id"], Request.Params["date_start"], Request.Params["date_end"]);

            renderListXML(list, "Reward");
        }

        private NameValueCollection getCriterion()
        {

            return U.filter(Request.Params, "rewardCriterion")[0];
        }

        private NameValueCollection getReward()
        {

            NameValueCollection reward = U.filter(Request.Params, "reward")[0];
            return reward;
        }

        private string[] getReward_ids()
        {
            return Request.Params.GetValues("reward_ids");
        }

        private static Hashtable privilegeMap = null;

        override protected Hashtable getPrivilegeMap()
        {
            if (privilegeMap == null)
            {
                privilegeMap = new Hashtable();

                privilegeMap.Add("list", (short)8101);
                privilegeMap.Add("create", (short)8102);
                privilegeMap.Add("update", (short)8103);
                privilegeMap.Add("delete", (short)8104);
                privilegeMap.Add("queryEmployeeReward", (short)8108);
            }

            return privilegeMap;
        }
    }
}
